const setCookie=((key:string,value:string,exDays:number)=>{
    if(window.localStorage){
        window.localStorage.setItem(key,value)
    }
    else{
        if(key.indexOf("=") !== -1) {
            throw new Error("Cookie不支持key中使用等号【=】, key:" + key)
        }
        let exDate = new Date() // 获取时间
        exDate.setTime(exDate.getTime() + 24 * 60 * 60 * 1000 * exDays) // 保存的天数
        // 字符串拼接cookie
        // eslint-disable-next-line camelcase
        window.document.cookie = key + '=' + value + ';path=/;expires=' + exDate.toString()
    }
})
const getCookie=((key:string)=>{
    let value:any
    if(window.localStorage){
        value=window.localStorage.getItem(key)
    }
    else{
        if (document.cookie.length > 0) {
            // 这里显示的格式需要切割一下自己可输出看下
            var arr = document.cookie.split('; ')
            for (let i = 0; i < arr.length; i++) {
                let arr2 = arr[i].split('=') // 再次切割
                // 判断查找相对应的值
                if (arr2[0] === key) {
                    value = arr2[1];
                    for (let j = 2; j < arr2.length; j++) {
                        value += '=' + arr2[j];
                    }
                    return value;
                }
            }
        }
    }
    return value
})

const deleteCookie=((key:string)=>{
    if(window.localStorage){
        window.localStorage.removeItem(key)
    }else{
        setCookie(key,"",-1)
    }
})

let storage={
    set:function (key:string,value:string){
        setCookie(key,value,7)
    },
    get:function (key:string){
        return getCookie(key)
    },
    remove:function (key:string){
        deleteCookie(key)
    }
}
export default storage;